package com.szmmsoft.mp.common.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.szmmsoft.mp.common.exception.BusinessCode;
import com.szmmsoft.mp.common.until.LoginUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.MDC;

import java.io.Serializable;
import java.util.Date;

/**
 * Description of the class.
 *
 * @author ethan
 * @version 1.0.0
 * @since 2024-10-12
 */
@Data
public class R<T> implements Serializable {
    @ApiModelProperty("提示消息")
    private String msg;
    @ApiModelProperty("返回数据")
    private T data;

    @ApiModelProperty("返回code")
    private Integer code;

    @ApiModelProperty("请求ID")
    private String requestId;

    @ApiModelProperty("返回时间")
    private Date time;

    public R(Integer code, String msg, T t) {
        this.code = code;
        this.msg = msg;
        this.data = t;
    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R() {
    }

    public R(Integer code, String msg, String requestId) {
        this.code = code;
        this.msg = msg;
        this.requestId = requestId;
        this.time = new Date();
    }

    /**
     * 成功
     *
     * @param data
     * @return
     * @param <T>
     */
    public static <T> R<T> ok(T data) {
        R<T> r = new R<>(BusinessCode.SUCESS.getCode(), BusinessCode.SUCESS.getMsg(), data);
        return r;
    }

    /**
     * 错误
     *
     * @param code
     * @param msg
     * @param requestId
     * @return
     * @param <T>
     */
    public static <T> R<T> error(Integer code, String msg,String requestId) {
        R<T> r = new R<>(code, msg,requestId);
        return r;
    }

    /**
     * 错误
     *
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> R<T> error(String msg,String requestId) {
        return error(BusinessCode.INTERNAL_SERVER_ERROR.getCode(),msg,requestId);
    }

    /**
     * 是否成功
     *
     * @return
     */
    @JsonIgnore
    public boolean isOk() {
        return getCode() == BusinessCode.SUCESS.getCode();
    }


}
